package com.mcxx.modules.miniSalvation.order.service.read;

import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.miniSalvation.order.dao.read.NoticeBoardDetailReadDao;
import com.mcxx.modules.miniSalvation.order.dto.NoticeBoardDetailDTO;
import com.mcxx.modules.miniSalvation.order.dto.OrderDTO;
import com.mcxx.modules.miniSalvation.order.dto.OrderListForChoiceDTO;
import com.mcxx.modules.miniSalvation.order.entity.NoticeBoardDetailEntity;
import com.mcxx.modules.miniSalvation.order.param.OrderListSearchParam;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("noticeBoardDetailReadService")
@TaTransactional(readOnly = true)
public class NoticeBoardDetailReadService extends BaseService {

    @Autowired
    private NoticeBoardDetailReadDao noticeBoardDetailReadDao;



    public Page findSaveObject(OrderListSearchParam param, PageParam pageParam, UserAccountVo userAccountVo){

        QueryParamUtil.setBaseParamValue(param, userAccountVo);
        noticeBoardDetailReadDao.beginPager(pageParam);
        List<OrderListForChoiceDTO> list = noticeBoardDetailReadDao.queryObject(param);
        return noticeBoardDetailReadDao.endPager(list);
    }


    public Page findEntrustObject(OrderListSearchParam param, PageParam pageParam){
        noticeBoardDetailReadDao.beginPager(pageParam);
        List<OrderListForChoiceDTO> list = noticeBoardDetailReadDao.queryEntrustObject(param);
        return noticeBoardDetailReadDao.endPager(list);
    }


    public NoticeBoardDetailEntity queryDetailByOrderId(String batchId, String orderId){
        return noticeBoardDetailReadDao.queryDetailByOrderId(batchId, orderId);
    }



    public Map<String, List<String>> getIdByIdAndFamilyNums(List<String> batchNoList){
        Map<String, List<String>> rstMap = new HashMap<>();

        List<String> yesObjectBatchNoList = noticeBoardDetailReadDao.getIdByIdAndFamilyNums(batchNoList);
        if(yesObjectBatchNoList.size() == 0){

            rstMap.put("noObject", batchNoList);
            return rstMap;
        }
        if(yesObjectBatchNoList.size() == batchNoList.size()){

            rstMap.put("yesObject", batchNoList);
            return rstMap;
        }

        List<String> noObjectBatchNoList = batchNoList.stream().filter(batchNo->yesObjectBatchNoList.contains(batchNo) == false).collect(Collectors.toList());

        rstMap.put("yesObject", yesObjectBatchNoList);
        rstMap.put("noObject", noObjectBatchNoList);

        return rstMap;
    }


    public List<OrderDTO> getOrderStateAndVersions(List<String> batchNoList,String tableTop){
        return noticeBoardDetailReadDao.getOrderStateAndVersions(batchNoList,tableTop);
    }


    public Map<String, List<String>> getNoByNoAndFamilyNums(List<String> batchNoList){
        Map<String, List<String>> rstMap = new HashMap<>();

        List<String> yesObjectBatchNoList = noticeBoardDetailReadDao.getNoByNoAndFamilyNums(batchNoList);
        if(yesObjectBatchNoList.size() == 0){

            rstMap.put("noObject", batchNoList);
            return rstMap;
        }
        if(yesObjectBatchNoList.size() == batchNoList.size()){

            rstMap.put("yesObject", batchNoList);
            return rstMap;
        }

        List<String> noObjectBatchNoList = batchNoList.stream().filter(batchNo->yesObjectBatchNoList.contains(batchNo) == false).collect(Collectors.toList());

        rstMap.put("yesObject", yesObjectBatchNoList);
        rstMap.put("noObject", noObjectBatchNoList);

        return rstMap;
    }


    public List<NoticeBoardDetailDTO> queryObjectByBatchId(OrderListSearchParam param){
        return noticeBoardDetailReadDao.queryObjectByBatchId(param);
    }

    public NoticeBoardDetailDTO queryTmpFinishedUnderAgeFamily(OrderListSearchParam param){
        return noticeBoardDetailReadDao.queryTmpFinishedUnderAgeFamily(param);
    }

    public List<NoticeBoardDetailDTO> queryTownMiniObjectByBatchId(OrderListSearchParam param){
        return noticeBoardDetailReadDao.queryTownMiniObjectByBatchId(param);
    }

    public List<NoticeBoardDetailDTO> queryCountyMiniObjectByBatchId(OrderListSearchParam param){
        return noticeBoardDetailReadDao.queryCountyMiniObjectByBatchId(param);
    }

    public NoticeBoardDetailDTO queryUnfinishUnderAgeFamily(OrderListSearchParam param){
        return noticeBoardDetailReadDao.queryUnfinishUnderAgeFamily(param);
    }

    public NoticeBoardDetailDTO queryFinishedUnderAgeFamily(OrderListSearchParam param){
        return noticeBoardDetailReadDao.queryFinishedUnderAgeFamily(param);
    }

    public List<NoticeBoardDetailEntity> queryAreaByBatchId(String batchId){
        return noticeBoardDetailReadDao.queryAreaByBatchId(batchId);
    }

    public NoticeBoardDetailDTO selectHasObjection(String orderId){
        return noticeBoardDetailReadDao.selectHasObjection(orderId);
    }


    public List<NoticeBoardDetailDTO> queryNoticeByOrderId(String orderId){
        return noticeBoardDetailReadDao.queryNoticeByOrderId(orderId);
    }
}
